// pages/songDetail/songDetail.js
import PubSub from 'pubsub-js';
import request from '../../static/utils/request'
//获取全局实例
const appInstance = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay: false,
    musicId: '',
    song: {}, //歌曲详情对象
    durationTime: '00:00', //总时长
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //options路由跳转参数
    let musicId = options.song;
    this.setData({
      musicId: musicId
    })
    this.getMusicInfo(musicId);
    //判断当前页面音乐是否在播放
    if (appInstance.globalData.isMusicPlay && appInstance.globalData.musicId === musicId) {
      //修改当前页面音乐播放状态
      this.setData({
        isPlay: true
      })
    }
    //创建控制音乐播放实例对象
    this.backgroundAudioManager = wx.getBackgroundAudioManager();
    //监视音乐播放与暂停
    this.backgroundAudioManager.onPlay(() => {
      //修改音乐播放状态
      this.changePlayState(true);

      appInstance.globalData.musicId = musicId;
    });
    this.backgroundAudioManager.onPause(() => {
      this.changePlayState(false);
    });
    this.backgroundAudioManager.onStop(() => {
      this.changePlayState(false);
    });
  },
  async getMusicInfo(musicId) {
    let songData = await request('/song/detail', {
      ids: musicId
    });
    // console.log(songData);
    this.setData({
      song: songData.songs[0],
      // durationTime: durationTime
    })
    //动态修改窗口标题
    wx.setNavigationBarTitle({
      title: this.data.song.name
    })
  },
  handleMusicPlay() {
    let isPlay = !this.data.isPlay;
    // this.setData({
    //   isPlay: !this.data.isPlay
    // })
    let {
      musicId
    } = this.data;
    this.musicControl(isPlay, musicId);
  },
  async musicControl(isPlay, musicId) {
    if (isPlay) {
      let musicLinkData = await request('/song/url', {
        id: musicId
      })
      // console.log(musicLinkData);
      let musicLink = musicLinkData.data[0].url;
      if (musicLink === null) {
        wx.showToast({
          title: '请开通会员后听取',
          icon: 'none'
        })
        return;
      }
      //歌曲播放
      this.backgroundAudioManager.src = musicLink;
      this.backgroundAudioManager.title = this.data.song.name; //不设置音频名不能播放歌曲 
    } else { //音乐暂停
      this.backgroundAudioManager.pause();
    }
  },
  //修改播放状态
  changePlayState(isPlay) {
    this.setData({
      isPlay: isPlay
    })
    //修改全局播放状态
    appInstance.globalData.isMusicPlay = isPlay;
  },
  handleSwitch(event){
      //切换类型
      let type = event.currentTarget.id;

      //关闭当前播放音乐
      this.backgroundAudioManager.stop();
      PubSub.publish('switchMusic',type)
        //订阅来自recommendSong页面
    PubSub.subscribe('musicId',(msg,musicId) => {
      //获取歌曲
      this.getMusicInfo(musicId);
      //自动播放当前音乐
      this.musicControl(true,musicId);
      //取消订阅 订阅消息是累加的 当拿到消息后取消订阅，相当于清除订阅池里面的消息
      // console.log(111);
      // console.log(musicId);
      PubSub.unsubscribe('musicId');
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})